﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;

namespace eProject.Web.UI.Admin
{
    public partial class PlanesManagementProgram : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateControls();
        }
        private void PopulateControls()
        {
            if (Page.IsPostBack) return;
            BindPlanesSearch();
        }
        private void BindAirports()
        {
            try
            {
                grvPlanes.DataSource = AirPorts.GetAll();
                grvPlanes.DataBind();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        private void BindPlanesSearch()
        {
            try
            {
                bool delete = cbDelete.Checked;
                string text = txtSearchText.Text.Trim().ToString();
                grvPlanes.DataSource = Planes.GetSearch(text, delete);
                grvPlanes.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<Company> BindCompany()
        {
            return Company.GetAll();
        }

        protected void grvPlanes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvPlanes.PageIndex = e.NewPageIndex;
            grvPlanes.DataBind();
            BindPlanesSearch();
        }

        protected void grvPlanes_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = int.Parse(grvPlanes.DataKeys[e.NewEditIndex].Value.ToString());
            Response.Redirect("PlanesManagementEdit.aspx?Id=" + id);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindPlanesSearch();
        }
        protected void grvPlanes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(grvPlanes.DataKeys[e.RowIndex].Value.ToString());
            Planes plane = new Planes(id);
            plane.IsDeleted = true;
            plane.Save();
            BindPlanesSearch();
        }
        protected void grvPlanes_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = int.Parse(grvPlanes.DataKeys[e.RowIndex].Value.ToString());
            Planes plane = new Planes(id);
            plane.IsDeleted = false;
            plane.Save();
            BindPlanesSearch();
        }
    }
}